package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.business.domain.JtAppUser;
import com.ruoyi.business.service.IJtAppUserService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.JtDistributor;
import com.ruoyi.business.service.IJtDistributorService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分销商信息Controller
 * 
 * @author Alan Zhang
 * @date 2025-03-31
 */
@Controller
@RequestMapping("/business/distributor")
public class JtDistributorController extends BaseController
{
    private String prefix = "business/distributor";

    @Autowired
    private IJtDistributorService jtDistributorService;
    @Autowired
    private IJtAppUserService jtAppUserService;

    @RequiresPermissions("business:distributor:view")
    @GetMapping()
    public String distributor()
    {
        return prefix + "/distributor";
    }

    /**
     * 查询分销商信息列表
     */
    @RequiresPermissions("business:distributor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JtDistributor jtDistributor)
    {
        startPage();
        List<JtDistributor> list = jtDistributorService.selectJtDistributorList(jtDistributor);
        return getDataTable(list);
    }

    /**
     * 导出分销商信息列表
     */
    @RequiresPermissions("business:distributor:export")
    @Log(title = "分销商信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JtDistributor jtDistributor)
    {
        List<JtDistributor> list = jtDistributorService.selectJtDistributorList(jtDistributor);
        ExcelUtil<JtDistributor> util = new ExcelUtil<JtDistributor>(JtDistributor.class);
        return util.exportExcel(list, "分销商信息数据");
    }

    /**
     * 新增分销商信息
     */
    @RequiresPermissions("business:distributor:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存分销商信息
     */
    @RequiresPermissions("business:distributor:add")
    @Log(title = "分销商信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JtDistributor jtDistributor)
    {
        return toAjax(jtDistributorService.insertJtDistributor(jtDistributor));
    }

    /**
     * 修改分销商信息
     */
    @RequiresPermissions("business:distributor:edit")
    @GetMapping("/edit/{distributorId}")
    public String edit(@PathVariable("distributorId") Long distributorId, ModelMap mmap)
    {
        JtDistributor jtDistributor = jtDistributorService.selectJtDistributorByDistributorId(distributorId);
        mmap.put("jtDistributor", jtDistributor);
        return prefix + "/edit";
    }

    /**
     * 修改保存分销商信息
     */
    @RequiresPermissions("business:distributor:edit")
    @Log(title = "分销商信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JtDistributor jtDistributor)
    {
        return toAjax(jtDistributorService.updateJtDistributor(jtDistributor));
    }

    /**
     * 删除分销商信息
     */
    @RequiresPermissions("business:distributor:remove")
    @Log(title = "分销商信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jtDistributorService.deleteJtDistributorByDistributorIds(ids));
    }


    /**
     * 修改分销商信息
     */
    @RequiresPermissions("business:distributor:review")
    @GetMapping("/review/{distributorId}")
    public String review(@PathVariable("distributorId") Long distributorId, ModelMap mmap)
    {
        JtDistributor jtDistributor = jtDistributorService.selectJtDistributorByDistributorId(distributorId);
        mmap.put("jtDistributor", jtDistributor);
        return prefix + "/review";
    }

    /**
     * 修改保存分销商信息
     */
    @RequiresPermissions("business:distributor:review")
    @Log(title = "分销商信息-审核", businessType = BusinessType.UPDATE)
    @PostMapping("/review")
    @ResponseBody
    public AjaxResult reviewSave(JtDistributor jtDistributor)
    {
        JtDistributor distributorDb = jtDistributorService.selectJtDistributorByDistributorId(jtDistributor.getDistributorId());
        if(distributorDb.getAuditStatus() != 0){
            return AjaxResult.error("该分销商已审核");
        }
        if(StringUtils.isEmpty(jtDistributor.getAuditReason())){
            return AjaxResult.error("请填写审核原因");
        }
        return jtDistributorService.auditJtDistributor(jtDistributor);
    }

    /**
     * 用户绑定分销商  信息
     */
    @RequiresPermissions("business:distributor:review")
    @Log(title = "分销商信息-创建账号", businessType = BusinessType.UPDATE)
    @PostMapping("/create/user/{distributorId}")
    @ResponseBody
    public AjaxResult createUser(@PathVariable("distributorId") Long distributorId)
    {
        return jtDistributorService.createSysUser(distributorId);
    }

    /**
     * 用户 绑定分销商信息
     */
    @RequiresPermissions("business:distributor:info")
    @GetMapping("/info")
    public String info(ModelMap mmap)
    {
        JtAppUser param = new JtAppUser();
        param.setSysUserId(ShiroUtils.getUserId());
        List<JtAppUser> jtAppUserList = jtAppUserService.selectJtAppUserList(param);
        if(jtAppUserList.size() ==0){
            return prefix + "/info";
        }
        JtAppUser jtAppUser = jtAppUserList.get(0);
        if(jtAppUser.getSysUserId() != null) {
            JtDistributor distributorParam = new JtDistributor();
            param.setUserId(jtAppUser.getUserId());

            List<JtDistributor> list = jtDistributorService.selectJtDistributorList(distributorParam);
            JtDistributor jtDistributor = null;
            if (list.size() > 0) {
                jtDistributor = list.get(0);
            }
            mmap.put("jtDistributor", jtDistributor);
        }
        return prefix + "/info";
    }

}
